import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import dts from 'vite-plugin-dts'

const VUE_FILE = /\.vue$/i

export default defineConfig({
  build: {
    minify: false,
    cssMinify: false,
    cssCodeSplit: true,
    outDir: 'dist',
    lib: {
      entry: 'src/index.ts',
      formats: ['es']
    },
    rollupOptions: {
      external: ['vue', '@vueuse/core', 'lodash-es'],
      output: {
        format: 'es',
        preserveModules: true,
        sourcemap: true,
        entryFileNames(chunkInfo) {
          const { name, facadeModuleId } = chunkInfo

          if (VUE_FILE.test(name)) {
            const { searchParams } = new URL(facadeModuleId)
            if (searchParams.has('vue') && searchParams.get('type') === 'script') {
              return name.replace(VUE_FILE, '_.mjs')
            }
            return name.replace(VUE_FILE, '.mjs')
          }

          return '[name].mjs'
        }
      }
    }
  },
  plugins: [
    vue(),
    dts({
      entryRoot: 'src',
      outputDir: 'dist',
      tsConfigFilePath: 'tsconfig.json',
      clearPureImport: true,
      rollupTypes: true,
      skipDiagnostics: true
    })
  ]
})
